package com.codedancer.campus.manager.api.school;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.school.ro.SchoolAddOrUpdateRo;
import com.codedancer.campus.manager.api.school.ro.SchoolPageSearchRo;
import com.codedancer.campus.manager.service.ISchoolService;
import com.codedancer.campus.manager.service.bo.school.SchoolAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.school.SchoolPageSearchBo;
import com.codedancer.campus.manager.service.vo.school.SchoolPageVo;
import com.codedancer.campus.manager.service.vo.school.SchoolVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/school")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "学校Api", description = "学校Api", tags = "学校Api")
public class SchoolApi extends BaseApi {

    @Autowired
    private ISchoolService schoolService;

    @PostMapping("/add")
    @ApiOperation("添加学校")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> add(@RequestBody @Valid @ApiParam(value = "学校新增Ro", required = true)
        SchoolAddOrUpdateRo schoolAddOrUpdateRo) {
        SchoolAddOrUpdateBo schoolAddOrUpdateBo = BeanUtil.copyProperties(schoolAddOrUpdateRo,
            SchoolAddOrUpdateBo.class);
        schoolService.add(schoolAddOrUpdateBo);
        return success();
    }

    @PostMapping("/update/{uuid}")
    @ApiOperation("修改学校")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> update(@RequestBody @Valid @ApiParam(value = "学校修改Ro", required = true)
        SchoolAddOrUpdateRo schoolAddOrUpdateRo,
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "学校uuid", required = true) String uuid) {
        SchoolAddOrUpdateBo schoolAddOrUpdateBo = BeanUtil.copyProperties(schoolAddOrUpdateRo,
            SchoolAddOrUpdateBo.class);
        schoolAddOrUpdateBo.setUuid(uuid);
        schoolService.update(schoolAddOrUpdateBo);
        return success();
    }

    @PutMapping("/enable/{uuid}")
    @ApiOperation("启用学校")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> enable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "学校uuid", required = true) String uuid) {
        schoolService.enable(uuid);
        return success();
    }

    @PutMapping("/disable/{uuid}")
    @ApiOperation("禁用学校")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> disable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "学校uuid", required = true) String uuid) {
        schoolService.disable(uuid);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除学校")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "学校uuid", required = true) String uuid) {
        schoolService.delete(uuid);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询学校")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<IPage<SchoolPageVo>> page(@ApiParam(value = "分页查询学校Ro", required = false)
        SchoolPageSearchRo schoolPageSearchRo) {
        SchoolPageSearchBo schoolPageSearchBo = BeanUtil.copyProperties(schoolPageSearchRo,
            SchoolPageSearchBo.class);
        return success(schoolService.page(schoolPageSearchBo));
    }

    @GetMapping("/all")
    @ApiOperation("获取所有学校")
    public ApiResponse<List<SchoolVo>> all(){
        return success(schoolService.all());
    }
}
